

#include <fstream>

#include "../mainctrl.h"
#include "../item/CItemMgr.h"
#include "pb_cfg_qqmall.pb.h"
#include "CQQMallMgr.h"
#include "../cmd/cmdapi.h"

int CQQMallMgr::LoadConfig(const char* szCfgPath)
{
    if (szCfgPath == NULL)
    {
        CT_ERROR(("Mall Cfg Path is null"));
        return RET_SYSTEM_FAIL;
    }

    pb_cfg_qqmall stMallCfg;
    std::fstream stFileInput(szCfgPath, std::ios::in | std::ios::binary);
    if (!stMallCfg.ParseFromIstream(&stFileInput))
    {
        CT_ERROR(("Failed to Parse Mall Cfg!"));
        return RET_SYSTEM_FAIL;
    }

    int iRet = 0;
    for (int i = 0; i < stMallCfg.item_to_goodsid_rows_size(); i++)
    {
        const pb_cfg_qqmall_pb_cfg_item_to_goodsId_unit & stGoodCfg = stMallCfg.item_to_goodsid_rows(i);

        CMallGoodsUnit stGoodUnit;
        stGoodUnit._uiGoodsId = stGoodCfg.goods_id();
        stGoodUnit._ucItemType = stGoodCfg.item_type();
        stGoodUnit._usItemCfgId = stGoodCfg.item_id();
        stGoodUnit._iItemCount = stGoodCfg.item_num();
        stGoodUnit._iRealCost = stGoodCfg.real_cost();
        stGoodUnit._iInitialCost = stGoodUnit._iRealCost;
        stGoodUnit._bIsNewGoods = stGoodCfg.is_new_goods();
        stGoodUnit._chAttr = stGoodCfg.attr();
        stGoodUnit._szDesc.Set("%s", stGoodCfg.desc().c_str());        
        stGoodUnit._iDailyTaskTriggerType = stGoodCfg.dailytask_trigger_type();
        stGoodUnit._iDailyTaskTriggerCount = stGoodCfg.dailytask_trigger_count();

        if (DEFAULT_GOODS_ID_32WAN != stGoodUnit._uiGoodsId)
        {
            iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetItemPrice(stGoodUnit._ucItemType, stGoodUnit._usItemCfgId,
            (unsigned short)stGoodUnit._iItemCount, ITEM_BUY_PRICE_GOLD, stGoodUnit._iInitialCost);
            if (iRet)
            {
                CT_ERROR(("%d, %d, %d", iRet, stGoodUnit._ucItemType, stGoodUnit._usItemCfgId));
                return iRet;
            }
        }

        iRet = _stConfig.AddGoodUnit(stGoodUnit);
        if (iRet)
        {
            CT_ERROR (("%d, %u", iRet, stGoodUnit._uiGoodsId));
            return iRet;
        }
    }

    for (int i = 0; i < stMallCfg.limited_time_rows_size(); i++)
    {
        const pb_cfg_qqmall_pb_cfg_limited_time_unit & stGoodCfg = stMallCfg.limited_time_rows(i);

        CQQLimitGoodsUnit stGoodUnit;
        stGoodUnit._uiGoodsId = stGoodCfg.goods_id();

        std::string sTime = stGoodCfg.start_time();
        if ('@' == sTime[0]) //距离开服天数
        {
            int iZoneOpenTime = CTTimeUtils::GetDayBeginSec(CMainCtrl::m_pstMain->m_stZoneOnlineData._stZoneData._iZoneOpenTime);

            CTStringUtils::Remove(sTime, '@');
            stGoodUnit._iValidTime   = iZoneOpenTime + (atoi(sTime.c_str()) - 1)* 86400;

            //可购买时间包括配置的当天
            sTime = stGoodCfg.end_time();
            CTStringUtils::Remove(sTime, '@');
            stGoodUnit._iInValidTime = iZoneOpenTime + atoi(sTime.c_str()) * 86400;
        }
        else
        {
            stGoodUnit._iValidTime   = CTTimeUtils::GetTsFromString (stGoodCfg.start_time().c_str());
            stGoodUnit._iInValidTime = CTTimeUtils::GetTsFromString (stGoodCfg.end_time().c_str());
        }

        stGoodUnit._iMaxBuyCnt = stGoodCfg.max_cnt();
        stGoodUnit._bFirstDaySendTips = stGoodCfg.send_tips();

        iRet = _stConfig.AddLimitGoodCfg (stGoodUnit, CMainCtrl::m_pstMain->m_iNow);
        if (iRet)
        {
            CT_ERROR (("%d, %u, %d, %d", iRet, stGoodUnit._uiGoodsId, stGoodUnit._iValidTime, stGoodUnit._iInValidTime));
            return iRet;
        }

        CT_TRACE (("%d, %d, %d, %d", stGoodUnit._uiGoodsId, stGoodUnit._iValidTime, stGoodUnit._iInValidTime, stGoodUnit._iMaxBuyCnt));
    }

    for (int i = 0; i < stMallCfg.hot_goods_rows_size(); i++)
    {
        const pb_cfg_qqmall_pb_cfg_hot_goods_unit & stGoodCfg = stMallCfg.hot_goods_rows(i);

        iRet = _stConfig.AddGoodCfg(MALL_TAB_HOT, stGoodCfg.goods_id());
        if (iRet)
        {
            CT_ERROR (("%d, %d", iRet, stGoodCfg.goods_id()));
            return iRet;
        }
    }

    for (int i = 0; i < stMallCfg.prop_rows_size(); i++)
    {
        const pb_cfg_qqmall_pb_cfg_prop_unit & stGoodCfg = stMallCfg.prop_rows(i);

        iRet = _stConfig.AddGoodCfg(MALL_TAB_PROP, stGoodCfg.goods_id());
        if (iRet)
        {
            CT_ERROR (("%d, %d", iRet, stGoodCfg.goods_id()));
            return iRet;
        }
    }

    for (int i = 0; i < stMallCfg.crystal_rows_size(); i++)
    {
        const pb_cfg_qqmall_pb_cfg_crystal_unit & stGoodCfg = stMallCfg.crystal_rows(i);

        iRet = _stConfig.AddGoodCfg(MALL_TAB_CRYSTAL, stGoodCfg.goods_id());
        if (iRet)
        {
            CT_ERROR (("%d, %d", iRet, stGoodCfg.goods_id()));
            return iRet;
        }
    }

    for (int i = 0; i < stMallCfg.quick_pay_rows_size(); i++)
    {
        const pb_cfg_qqmall_pb_cfg_quick_pay_unit & stGoodCfg = stMallCfg.quick_pay_rows(i);

        CQuickBuyGoodsUnit stUnit;
        stUnit._ucItemType = stGoodCfg.item_type();
        stUnit._usItemCfgId = stGoodCfg.item_id();

        //检查物品是否存在
        CItemConfigData stConfigData;
        iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetConfig(stUnit._ucItemType, stUnit._usItemCfgId, stConfigData);
        if (iRet)
        {
            CT_ERROR (("%d, %d, %d", iRet, stUnit._ucItemType, stUnit._usItemCfgId));
            return iRet;
        }

        iRet = _stConfig.AddQuickBuyGoodCfg(stUnit, stGoodCfg.goods_id());
        if (iRet)
        {
            CT_ERROR (("%d, %d, %d", iRet, stUnit._ucItemType, stUnit._usItemCfgId));
            return iRet;
        }
    }

    for (int i = 0; i < stMallCfg.mount_rows_size(); i++)
    {
        const pb_cfg_qqmall_pb_cfg_mount_unit & stGoodCfg = stMallCfg.mount_rows(i);

        //检查物品是否存在
        unsigned short usGoodsID = stGoodCfg.goods_id();
        iRet = _stConfig.AddGoodCfg(MALL_TAB_MOUNT, usGoodsID);
        if (iRet)
        {
            CT_ERROR (("%d, %d", iRet, usGoodsID));
            return iRet;
        }
    }

    for (int i = 0; i < stMallCfg.fashion_rows_size(); i++)
    {
        const pb_cfg_qqmall_pb_cfg_fashion_unit & stGoodCfg = stMallCfg.fashion_rows(i);

        //检查物品是否存在
        unsigned short usGoodsID = stGoodCfg.goods_id();
        iRet = _stConfig.AddGoodCfg(MALL_TAB_FASHION, usGoodsID);
        if (iRet)
        {
            CT_ERROR (("%d, %d", iRet, usGoodsID));
            return iRet;
        }
    }

    //a.限时购买的商品是不可能出现在其它购买的地方. b).相同的时间段内，不同出售相同的商品。 加个检查
    if (!_stConfig.IsLimitGoodsValid())
    {
        CT_ERROR (("LimitGoodsValid config err"));
        return ERROR_QQMALL_GOODS_ID;
    }

    RefreshSendTipsGoodID (CMainCtrl::m_pstMain->m_iNow);

    return RET_OK;
}

void CQQMallMgr::ProcCrossDay (int iNowTime)
{
    RefreshSendTipsGoodID(iNowTime);

    if (0 == _uiSendTipsGoodsId)
    {
        return ;
    }

    MsgTips stTips;
    GetNewGoodsTips (stTips);

    CMainCtrl::m_pstMain->SendOnlineTips (stTips);
}

void CQQMallMgr::CheckAndSendTips (unsigned int uiGID)
{
    if (0 == _uiSendTipsGoodsId)
    {
        return ;
    }

    MsgTips stTips;
    GetNewGoodsTips (stTips);

    CMainCtrl::m_pstMain->SendOnlineTips (uiGID, stTips);
}

void CQQMallMgr::GetNewGoodsTips (MsgTips & stTips)
{
    stTips.set_type(TIPS_MALL_NEW_GOODS);
    GetMallGoodsInfo (_uiSendTipsGoodsId, stTips.add_new_goods());
}

void CQQMallMgr::GetMallTabContext (CUserQQMall & stUserMall, EN_MALL_TAB_TYPE enTab, MsgAnsQQMall & stAns)
{
    stAns.set_tab_type(enTab);
    switch (enTab)
    {
    case MALL_TAB_LIMIT:
        {
            int iNowTime = CMainCtrl::m_pstMain->m_iNow;

            //先清除掉之前买的，已过限购时间的物品
            stUserMall.DelExpiedGoods(iNowTime);

            int iLimitGoodsCount = 0;
            for (int i = 0; i < _stConfig._stLimitCfg.Size(); i++)
            {
                CQQLimitGoodsUnit & stGoods = _stConfig._stLimitCfg[i];
                if (iNowTime >= stGoods._iInValidTime || iNowTime <= stGoods._iValidTime)
                {
                    continue;
                }

                MsgMallGoods * pstMsgGood = stAns.add_infos();
                GetMallGoodsInfo (stGoods._uiGoodsId, pstMsgGood);
                pstMsgGood->set_valid_time(stGoods._iValidTime);
                pstMsgGood->set_invalid_time(stGoods._iInValidTime);
                pstMsgGood->set_can_buy_cnt(stGoods._iMaxBuyCnt - stUserMall.GetGoodsBuiedNums(stGoods._uiGoodsId));
                iLimitGoodsCount += 1;
            }

            //检查配置是否超标，超标的话，直接清空限价商品
            if (iLimitGoodsCount > MAX_LIMIT_GOODS_MEANWHILE)
            {
                CT_ERROR(("%d, %d, %d", iLimitGoodsCount, MAX_LIMIT_GOODS_MEANWHILE, iNowTime));
                stAns.Clear();
            }
        }
        break;

    case MALL_TAB_HOT:
        {
            for (int i = 0; i < _stConfig._stHotCfg.Size(); i++)
            {
                GetMallGoodsInfo (_stConfig._stHotCfg[i], stAns.add_infos());
            }
        }
        break;

    case MALL_TAB_CRYSTAL:
        {
            for (int i = 0; i < _stConfig._stCrystalCfg.Size(); i++)
            {
                GetMallGoodsInfo (_stConfig._stCrystalCfg[i], stAns.add_infos());
            }
        }
        break;

    case MALL_TAB_PROP:
        {
            for (int i = 0; i < _stConfig._stPropCfg.Size(); i++)
            {
                GetMallGoodsInfo (_stConfig._stPropCfg[i], stAns.add_infos());
            }
        }
        break;
    case MALL_TAB_MOUNT:
        {
            for (int i = 0; i < _stConfig._stMountCfg.Size(); i++)
            {
                unsigned int uiGoodsId = _stConfig._stMountCfg[i];
                MsgMallGoods * pstMsg = stAns.add_infos();
                GetMallGoodsInfo (uiGoodsId, pstMsg);
                pstMsg->set_can_buy_cnt(stUserMall.GetCanBuyMountCnt(uiGoodsId));
            }
        }
        break;
    case MALL_TAB_FASHION:
        {
            for (int i = 0; i < _stConfig._stFashionCfg.Size(); i++)
            {
                unsigned int uiGoodsId = _stConfig._stFashionCfg [i];
                MsgMallGoods * pstMsg = stAns.add_infos();
                GetMallGoodsInfo (uiGoodsId, pstMsg);
                pstMsg->set_can_buy_cnt(stUserMall.GetCanBuyMountCnt(uiGoodsId));
            }
        }
        break;
    default:
        return;
    }
}

void CQQMallMgr::GetMallGoodsInfo (unsigned int uiGoodsId, MsgMallGoods * pstMsg)
{
    const CMallGoodsUnit & stGoodCfg = _stConfig.GetGoodUnit (uiGoodsId);
    pstMsg->set_item_type(stGoodCfg._ucItemType);
    pstMsg->set_item_config_id(stGoodCfg._usItemCfgId);
    pstMsg->set_item_count(stGoodCfg._iItemCount);
    pstMsg->set_goods_id(stGoodCfg._uiGoodsId);
    pstMsg->set_real_cost(stGoodCfg._iRealCost);
    pstMsg->set_attr(stGoodCfg._chAttr);
    pstMsg->set_is_new_good(stGoodCfg._bIsNewGoods);
    pstMsg->set_initial_cost(stGoodCfg._iInitialCost);
}

int CQQMallMgr::GetMallDropGoods (unsigned int uiGoodsId, int iNum, CDropItem & stDropItem)
{
    if (!_stConfig.IsValidGoods(uiGoodsId))
    {
        return ERROR_QQMALL_GOODS_NONE;
    }

    if (iNum <= 0)
    {
        return ERROR_QQMALL_GOODS_NONE;
    }

    const CMallGoodsUnit & stGoodUnit = _stConfig.GetGoodUnit (uiGoodsId);
    CDropItemData stData;
    stData._ucItemType = stGoodUnit._ucItemType;
    stData._usItemConfigID = stGoodUnit._usItemCfgId;
    stData._iItemCount = stGoodUnit._iItemCount * iNum;

    stDropItem.Drop(stData);

    return RET_OK;
}

int CQQMallMgr::GetMallGoodsVipExp (unsigned int uiGoodsId, int iNum)
{
    int iVipExp = 0;
    if (!_stConfig.IsValidGoods(uiGoodsId) || iNum <= 0)
    {
        return iVipExp;
    }

    const CMallGoodsUnit & stGoodUnit = _stConfig.GetGoodUnit (uiGoodsId);
    if (stGoodUnit._iRealCost <= 0)
    {
        //如果商品没有折后价，则按照原价统计
        iVipExp = stGoodUnit._iInitialCost * iNum;
    }
    else
    {
        //VIP成长值中记录商品的折后价
        iVipExp = stGoodUnit._iRealCost * iNum;;
    }

    return iVipExp;
}

bool CQQMallMgr::IsValidGoodsId (unsigned int uiGoodsId)
{
    return _stConfig.IsValidGoods(uiGoodsId);
}

int CQQMallMgr::InitBuyGoodsCmd (unsigned int uiGoodsId, int iNum, int iMaxNum, CCmdApi * pstCmd)
{
    if (!IsValidGoodsId(uiGoodsId))
    {
        return ERROR_QQMALL_GOODS_ID;
    }

    const CMallGoodsUnit & stGoodCfg = _stConfig.GetGoodUnit(uiGoodsId);

    pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstCmd->stReq._ucReqType = CReqApi::API_TENCENT;
    pstCmd->stReq._szTApiName.Set("/v3/pay/buy_goods");

    CTSizeString<1024> szTmp;
    szTmp.Set("%d", iNum*stGoodCfg._iRealCost);
    pstCmd->stReq._stTApiKeyValue.SetValue("amt", szTmp());

    szTmp.Set("%d", CMainCtrl::m_pstMain->m_iNow);
    pstCmd->stReq._stTApiKeyValue.SetValue("ts", szTmp());

    szTmp.Set("%d*%d*%d", uiGoodsId, stGoodCfg._iRealCost, iNum);
    pstCmd->stReq._stTApiKeyValue.SetValue("payitem", szTmp());

    szTmp.Set("%d", iMaxNum);  //最多可购买数
    pstCmd->stReq._stTApiKeyValue.SetValue("max_num", szTmp());

    szTmp.Set("%s*%d", stGoodCfg._szDesc(), stGoodCfg._iItemCount);
    pstCmd->stReq._stTApiKeyValue.SetValue("goodsmeta", szTmp());

    CTSizeString<MAX_ICON_NAME_LEN> szIcoName;
    int iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetIconName(stGoodCfg._ucItemType, stGoodCfg._usItemCfgId, szIcoName);
    CHECK_RETURN(iRet);

    szTmp.Set("%s%s", CONF->QQMallIconPath(), szIcoName());
    pstCmd->stReq._stTApiKeyValue.SetValue("goodsurl", szTmp());

    szTmp.Set("%d", CONF->ZoneID);
    pstCmd->stReq._stTApiKeyValue.SetValue("zoneid", szTmp());

    return 0;
}

unsigned int CQQMallMgr::GetQuickBuyGoodsId (unsigned char ucItemType, unsigned short usItemCfgId)
{
    CQuickBuyGoodsUnit stUnit;
    stUnit._ucItemType = ucItemType;
    stUnit._usItemCfgId = usItemCfgId;

    int iIndex = _stConfig._stQuickBuyGoods.HashFind (stUnit);
    if (iIndex < 0)
    {
        return 0;
    }

    return _stConfig._stQuickBuyGoods[iIndex];
}

int CQQMallMgr::CheckBuyLimit (CUserQQMall & stUserMall, unsigned int uiGoodsId, int iNums, int iNowTime)
{
    if (_stConfig.IsMountGoods(uiGoodsId))
    {
        if (stUserMall.GetCanBuyMountCnt(uiGoodsId) <= 0)
        {
            return ERROR_QQMALL_LIMTED_BUYNUMS;
        }
    }
    else
    {
        CQQLimitGoodsUnit stGoods;
        int iRet = _stConfig.GetLimitGoodsConfig (uiGoodsId, iNowTime, stGoods);
        if (iRet)
        {
            CT_ERROR(("%d, %d, %d", uiGoodsId, iNowTime, iRet));
            return iRet;
        }

        //先清除掉之前买的，已过限购时间的物品
        stUserMall.DelExpiedGoods(iNowTime);

        int iBuiedNums = stUserMall.GetGoodsBuiedNums(uiGoodsId);
        if (iBuiedNums == 0 && stUserMall._stLimitItems.Size() >= MAX_LIMIT_GOODS_MEANWHILE)
        {
            return ERROR_QQMALL_LIMTED_BUYNUMS;
        }

        if ((iBuiedNums + iNums) > stGoods._iMaxBuyCnt)
        {
            return ERROR_QQMALL_LIMTED_BUYNUMS;
        }
    }

    return 0;
}

int CQQMallMgr::CheckCanBuy (CUserQQMall & stUserMall, int iNowTime, MsgReqQQMall & stReq)
{
    unsigned int uiGoodsId = stReq.goods_id();
    if (!_stConfig.IsValidGoods(uiGoodsId))
    {
        return ERROR_QQMALL_GOODS_ID;
    }

    const CMallGoodsUnit & stCfg = _stConfig.GetGoodUnit(uiGoodsId);
    if (stCfg._iRealCost != stReq.price())
    {
        return ERROR_QQMALL_GOODS_PRICE;
    }

    switch (stReq.tab_type())
    {
    case MALL_TAB_LIMIT:
        {
            return CheckBuyLimit(stUserMall, stReq.goods_id(), stReq.num(), iNowTime);
        }
        break;

    case MALL_TAB_HOT:
        {
            for (int i = 0; i < _stConfig._stHotCfg.Size(); i++)
            {
                if (_stConfig._stHotCfg[i] == uiGoodsId)
                {
                    return 0;
                }
            }
        }
        break;

    case MALL_TAB_CRYSTAL:
        {
            for (int i = 0; i < _stConfig._stCrystalCfg.Size(); i++)
            {
                if (_stConfig._stCrystalCfg[i] == uiGoodsId)
                {
                    return 0;
                }
            }
        }
        break;

    case MALL_TAB_PROP:
        {
            for (int i = 0; i < _stConfig._stPropCfg.Size(); i++)
            {
                if (_stConfig._stPropCfg[i] == uiGoodsId)
                {
                    return 0;
                }
            }
        }
        break;
    }

    return ERROR_QQMALL_GOODS_UNRACK;
}

int CQQMallMgr::AddBuyedLimitGoods (CPlayerDetailInfoEx1 & stPlayerDetailEx1, unsigned int uiGoodsId, int iNums, int iNowTime)
{
    int iRet = RET_OK;
    CUserQQMall & stUserMall = stPlayerDetailEx1._stUserMall;

    unsigned char ucTabType = 0;
    if (_stConfig.IsMountGoods(uiGoodsId))
    {
        iRet = stUserMall.CheckThenAddMount (uiGoodsId);
        CHECK_RETURN(iRet);

        ucTabType = MALL_TAB_MOUNT;
    }
    else
    {
        iRet = CheckBuyLimit(stUserMall, uiGoodsId, iNums, iNowTime);
        CHECK_RETURN(iRet);

        CQQLimitGoodsUnit stGoods;
        iRet = _stConfig.GetLimitGoodsConfig (uiGoodsId, iNowTime, stGoods);
        CHECK_RETURN(iRet);

        //先清除掉之前买的，已过限购时间的物品
        stUserMall.DelExpiedGoods(iNowTime);

        //添加本次购买的
        bool bIsNewItem = true;
        for (int k = 0; k < stUserMall._stLimitItems.Size(); k++)
        {
            //同一时刻,相同商品只会出售一个
            if (uiGoodsId == stUserMall._stLimitItems[k]._uiGoodsId)
            {
                stUserMall._stLimitItems[k]._iBuiedNums += iNums;
                bIsNewItem = false;
            }
        }

        if (bIsNewItem)
        {
            if (stUserMall._stLimitItems.Size() >= MAX_LIMIT_GOODS_MEANWHILE)
            {
                return ERROR_QQMALL_GOODS_FULL;
            }

            CQQMallLimitItem stItem;
            stItem._uiGoodsId = uiGoodsId;

            if (stGoods._bLimitByDay)
            {
                stItem._iExpiredTime = CTTimeUtils::GetDayBeginSec(iNowTime) + 86400;  //按天限量
            }
            else
            {
                stItem._iExpiredTime = stGoods._iInValidTime;
            }

            stItem._iBuiedNums = iNums;
            stUserMall._stLimitItems.AddOneItem(stItem);
        }

        ucTabType = MALL_TAB_LIMIT;
    }

    SendLimitInfoToFront(stUserMall, stPlayerDetailEx1._uiGID, ucTabType);

    return 0;
}

bool CQQMallMgr::IsLimitGoods (unsigned int uiGoods)
{
    if (_stConfig.IsLimitGoods(uiGoods))
    {
        return true;
    }

    if (_stConfig.IsMountGoods(uiGoods))
    {
        return true;
    }

    return false;
}

void CQQMallMgr::SendLimitInfoToFront (CUserQQMall & stUserMall, unsigned int uiGID, unsigned char ucTabType)
{
    CEnpNetHead stEnpHead;
    int iRet = CMainCtrl::m_pstMain->m_stPlayerOnline.GetOnlinePlayerHandle(uiGID, stEnpHead);
    if (RET_OK != iRet)
    {
        CT_ERROR(("%d, %d", iRet, uiGID));
        return;
    }
    //推送限时商品的内容给前台
    PBMsgHead stMsgHead;
    stMsgHead.set_msgid(ID_MAIN_QQ_MALL);
    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);

    MsgAnsQQMall stAns;
    CMainCtrl::m_pstMain->m_stQQMallMgr.GetMallTabContext (stUserMall, (EN_MALL_TAB_TYPE)ucTabType, stAns);
    stAns.set_opt(1);

    CMainCtrl::m_pstMain->m_stEncodeBuf.Clear();
    CMainCtrl::m_pstMain->EncodePBMsg(stMsgHead, CMainCtrl::m_pstMain->m_stEncodeBuf);
    CMainCtrl::m_pstMain->EncodePBMsg(stAns, CMainCtrl::m_pstMain->m_stEncodeBuf);
    CMainCtrl::m_pstMain->AnswerToClient(stEnpHead, CMainCtrl::m_pstMain->m_stEncodeBuf,true);
}

int CQQMallMgr::GetMaxBuyNum (CUserQQMall & stUserMall, unsigned int uiGoodsId, int iNowTime)
{
    int iMaxBuyNum = 0;
    if (_stConfig.IsMountGoods(uiGoodsId))
    {
        iMaxBuyNum = stUserMall.GetCanBuyMountCnt(uiGoodsId);
    }
    else
    {
        CQQLimitGoodsUnit stGoods;
        int iRet = _stConfig.GetLimitGoodsConfig (uiGoodsId, iNowTime, stGoods);
        if (iRet)
        {
            CT_ERROR(("%d, %d, %d", uiGoodsId, iNowTime, iRet));
            return 0;
        }

        int iBuiedNums = stUserMall.GetGoodsBuiedNums(uiGoodsId);
        if (iBuiedNums == 0 && stUserMall._stLimitItems.Size() >= MAX_LIMIT_GOODS_MEANWHILE)
        {
            return 0;
        }

        iMaxBuyNum = CT_MAX((stGoods._iMaxBuyCnt - iBuiedNums), 0);
    }

    return iMaxBuyNum;
}

void CQQMallMgr::RefreshSendTipsGoodID (int iNowTime)
{
    _uiSendTipsGoodsId = 0;

    for (int i = 0; i < _stConfig._stLimitCfg.Size(); i++)
    {
        CQQLimitGoodsUnit & stGoods = _stConfig._stLimitCfg[i];
        if ((true == stGoods._bFirstDaySendTips) && (CTTimeUtils::IsSameDay(stGoods._iValidTime, iNowTime, 0)))
        {
            _uiSendTipsGoodsId = stGoods._uiGoodsId;
            break;
        }
    }

    return ;
}

const CMallGoodsUnit & CQQMallMgr::GetGoodUnit (unsigned int uiGoodsId)
{
    return _stConfig.GetGoodUnit(uiGoodsId);
}
